package com.example.myactspringboot.server;

import com.example.myactspringboot.bean.SysUser;
import com.example.myactspringboot.dao.SysUserMapper;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.UserTask;
import org.activiti.engine.delegate.DelegateExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Create 2023-09-23 08:36
 * @Description
 */
@Service
public class SysUserService {
    @Autowired
    @Qualifier(value = "sysUserMapper")
    private SysUserMapper sysUserMapper;

    public SysUser getUserByName(String name){
        SysUser userByName = sysUserMapper.getUserByName(name);
        return userByName;
    }

    public List<SysUser> getAllUser(){
        List<SysUser> allUser = sysUserMapper.getAllUser();
        return allUser;
    }

    public HashSet<String> getUserByRoleId(DelegateExecution execution){
        HashSet<String> candidateUserIds = new LinkedHashSet<>();
        FlowElement flowElement = execution.getCurrentFlowElement();
        if (flowElement != null && flowElement instanceof UserTask) {
            UserTask userTask = (UserTask) flowElement;
            if (userTask.getCandidateUsers() != null && userTask.getCandidateUsers().size()>0) {
                candidateUserIds.addAll(userTask.getCandidateUsers());
            } else if (userTask.getCandidateGroups() != null && userTask.getCandidateGroups().size()>0) {
                List<SysUser> userIds = sysUserMapper.getUserByRoleId(userTask.getCandidateGroups());
                List<String> userIdStrs = userIds.stream().map( SysUser::getId).collect(Collectors.toList());
                candidateUserIds.addAll(userIdStrs);
            }
            System.out.println("根据角色id获取用户id，返回给userTask");
            System.out.println(candidateUserIds);
        }

        return candidateUserIds;
    }
    public HashSet<String> getUserByRoleId(DelegateExecution execution,String roleId){
        System.out.println("根据角色分配用户");
        HashSet<String> candidateUserIds = new LinkedHashSet<>();
        ArrayList<String> objects = new ArrayList<>();
        objects.add(roleId);
        List<SysUser> userIds = sysUserMapper.getUserByRoleId(objects);
        List<String> userIdStrs = userIds.stream().map( SysUser::getId).collect(Collectors.toList());
        candidateUserIds.addAll(userIdStrs);
        return candidateUserIds;
    }

    public boolean compleTaskTo(DelegateExecution execution){
        System.out.println("任务会签总数："+execution.getVariable("nrOfInstances"));
        System.out.println("待会签任务数量："+execution.getVariable("nrOfActiveInstances"));
        System.out.println("已完成会签任务数量："+execution.getVariable("nrOfCompletedInstances"));
//设置权重
        Integer agree = (Integer) execution.getVariable("agree");//同意人数
        if(agree/(Integer) execution.getVariable("nrOfInstances")>0.5){
            System.out.println("同意人数："+agree);
            //设置会签结果 排他网关会用到
            execution.setVariable("status","true");
            return  true;
        }else{
            execution.setVariable("status","false");
            return  false;
        }
    }

}
